﻿using Dal.Iterfease;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Model.viewModel;

namespace Dal.infDal
{
    public class OrderDal: IOrderDal
    {
        private readonly AppDbContext _appDbContext;

        public OrderDal(AppDbContext appDbContext)
        {
            _appDbContext = appDbContext;
        }

        /// <summary>
        /// 生成订单
        /// </summary>
        /// <param name="ord"></param>
        /// <param name="onumber"></param>
        /// <returns></returns>
        public int AddOrder(OrderModel ord,out  string onumber)
        {
            //生成雪花编号
            var Onum = SnowID._idWorker;
            var number = Onum.NextId().ToString();
            //给两个表赋值
            ord.orderFormA.Onumber = number;
            ord.orderFormA.OrderTIM = DateTime.Now;

            _appDbContext.orderFormA.Add(ord.orderFormA);
            foreach (var item in ord.orderFormB)
            {
                item.Fanumber = number;
                _appDbContext.orderFormB.Add(item);
            }
            //方便查询
            onumber = number;
            return _appDbContext.SaveChanges();

        }

        /// <summary>
        /// 确认订单
        /// </summary>
        /// <param name="onumber"></param>
        /// <returns></returns>
        public List<PaymentModel> showPayment(string onumber)
        {
            var sql = from a in _appDbContext.orderFormA 
                      join b in _appDbContext.orderFormB on a.Onumber equals b.Fanumber
                      join c in _appDbContext.commodityInfos on b.Fcid equals c.Cid
                      join d in _appDbContext.siteInfo on a.Osid equals d.Sid
                      join da in _appDbContext.Ssx on d.shengid equals da.Xid
                      join db in _appDbContext.Ssx on d.shiid equals db.Xid
                      join dc in _appDbContext.Ssx on d.xianid equals dc.Xid
                      join e in _appDbContext.userInfos on a.Ouid equals e.Uid
                      where a.Onumber==onumber
                      select new PaymentModel
                      {
                          countprice = a.countprice,
                          Onumber=a.Onumber,
                          Padds=d.Sname+ d.Sphone+da.Xname+db.Xname+dc.Xname+d.Sadds,
                          Pcomm=c.Cname+c.Cversions+c.Ccolor

                      };

            return sql.ToList();
        }

    }
}
